package fr.ul.ade.secunde.ui;

import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;

import fr.ul.ade.R;
import fr.ul.ade.secunde.database.AdeWebContract;
import fr.ul.ade.secunde.util.Bundles;

/**
 * Fragment affichant les détails des cours d'un emploi du temps donné.
 * 
 * @author Michaël Schnell
 * @since Jul 2, 2013
 * 
 * @version 1.5
 */
public class LessonDetailsViewPagerFragment
		extends SherlockFragment
		implements LoaderCallbacks<Cursor> {

	public static LessonDetailsViewPagerFragment newInstance(long scheduleId,
			long lessonId) {
		final LessonDetailsViewPagerFragment fragment = new LessonDetailsViewPagerFragment();
		Bundle args = new Bundle();
		args.putLong(Bundles.LESSON_DETAILS_SCHEDULE_ID, scheduleId);
		args.putLong(Bundles.LESSON_DETAILS_ID, lessonId);
		fragment.setArguments(args);
		return fragment;
	}

	// --------------------------------------------------------
	//
	// Création et destruction
	//
	// --------------------------------------------------------

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_lesson_details_vp,
				container,
				false);
		return view;
	}

	// --------------------------------------------------------
	//
	// Chargement des données
	//
	// --------------------------------------------------------

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		Uri contentUri;
		String[] projection;
		String sortBy;

		long scheduleId = args.getLong(Bundles.LESSON_DETAILS_SCHEDULE_ID);

		switch (id) {
		default:
			return null;

		case ScheduleQuery._TOKEN:
			contentUri = AdeWebContract.Schedule.buildUri(scheduleId);
			projection = ScheduleQuery.PROJECTION;
			sortBy = AdeWebContract.Schedule.DEFAULT_SORT;
			break;

		case LessonQuery._TOKEN:
			contentUri = AdeWebContract.Lesson.buildLessonUri(scheduleId);
			projection = LessonQuery.PROJECTION;
			sortBy = AdeWebContract.Lesson.SORT_BY_ORDER;
		}

		return new CursorLoader(this.getActivity(), contentUri, projection,
				null, null, sortBy);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		/* Nothing to be done. */
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		if (this.getActivity() == null) {
			return;
		}// if

		switch (loader.getId()) {
		default:
			return;

		case ScheduleQuery._TOKEN:
			break;

		case LessonQuery._TOKEN:

		}
	}

	// --------------------------------------------------------
	//
	// Traitement des évènements
	//
	// --------------------------------------------------------

	// TODO Ajouter écouteur pour les changments des cours

	// --------------------------------------------------------
	//
	// Données chargées
	//
	// --------------------------------------------------------

	protected static interface ScheduleQuery {
		final int _TOKEN = 0x1;

		final String[] PROJECTION = { BaseColumns._ID,
				AdeWebContract.Schedule.SCHEDULE_NAME };

		final int _ID = 0;
		final int SCHEDULE_NAME = 1;
	}

	protected static interface LessonQuery {
		final int _TOKEN = 0x2;

		final String[] PROJECTION = { BaseColumns._ID,
				AdeWebContract.Lesson.LESSON_TITLE,
				AdeWebContract.Lesson.LESSON_DATE,
				AdeWebContract.Lesson.LESSON_START_TIME,
				AdeWebContract.Lesson.LESSON_END_TIME,
				AdeWebContract.Lesson.LESSON_ROOMS,
				AdeWebContract.Lesson.LESSON_TEACHERS,
				AdeWebContract.Lesson.LESSON_GROUPS,
				AdeWebContract.Lesson.LESSON_NOTE,
				AdeWebContract.Lesson.LESSON_CREATED,
				AdeWebContract.Lesson.LESSON_OLD_DATE,
				AdeWebContract.Lesson.LESSON_OLD_START_TIME,
				AdeWebContract.Lesson.LESSON_OLD_END_TIME,
				AdeWebContract.Lesson.LESSON_OLD_ROOMS,
				AdeWebContract.Lesson.LESSON_CHANGE,
				AdeWebContract.Lesson.LESSON_COLOR };

		final int _ID = 0;
		final int LESSON_TITLE = 1;
		final int LESSON_DATE = 2;
		final int LESSON_START_TIME = 3;
		final int LESSON_END_TIME = 4;
		final int LESSON_ROOMS = 5;
		final int LESSON_TEACHERS = 6;
		final int LESSON_GROUPS = 7;
		final int LESSON_NOTE = 8;
		final int LESSON_CREATED = 9;
		final int LESSON_OLD_DATE = 10;
		final int LESSON_OLD_START_TIME = 11;
		final int LESSON_OLD_END_TIME = 12;
		final int LESSON_OLD_ROOMS = 13;
		final int LESSON_CHANGE = 14;
		final int LESSON_COLOR = 15;
	}

	// --------------------------------------------------------
	//
	// Adaptateur pour le ViewPager
	//
	// --------------------------------------------------------

	protected static class LessonAdapter
			extends FragmentStatePagerAdapter {

		private Cursor data;

		public LessonAdapter(FragmentManager fm) {
			super(fm);
			this.data = new MatrixCursor(LessonQuery.PROJECTION, 0);
		}

		@Override
		public Fragment getItem(int position) {
			// TODO
			return null;
		}

		@Override
		public int getCount() {
			return this.data.getCount();
		}

	}

}// class LessonDetailsFragment